/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeatsSelectionView.java
 *
 * Created on Apr 4, 2010, 2:05:31 AM
 */
package ticketmaster.presentation.customer.purchase;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Collection;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import ticketmaster.businesslogic.SeatController;
import ticketmaster.businessobject.SeatBO;
import ticketmaster.businessobject.ShoppingCart;
import ticketmaster.businessobject.Ticket;
import ticketmaster.dao.DAOFactory;
import ticketmaster.dataobject.EventView;
import ticketmaster.dataobject.TicketRate;
import ticketmaster.presentation.customer.CustomerBasePanel;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Wei Liu
 */
public class SeatsSelectionView extends CustomerBasePanel
{

    private SeatsTable table;
    private ShoppingCart cart;
    private EventView event;
    private JPanel pnlTable;

    /** Creates new form SeatsSelectionView */
    public SeatsSelectionView(EventView event)
    {
        this.setEvent(event);
        initComponents();
        initTable();
        bindComboBox(event);
    }

    private void bindComboBox(EventView event)
    {
        cbTicketRate.removeAllItems();
        TicketRate ticketRate = new TicketRate();
        ticketRate.setEventID(event.getEventID());
        Collection<TicketRate> ticketRates = DAOFactory.getInstance().getTicketRateDAO().getTicketRateDO(ticketRate);

        for (TicketRate rate : ticketRates)
        {
            cbTicketRate.addItem(rate);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbTicketRate = new javax.swing.JLabel();
        cbTicketRate = new javax.swing.JComboBox();
        tfTicketNumber = new javax.swing.JTextField();
        lbTicketNumber = new javax.swing.JLabel();
        btnFindSeats = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnBook = new javax.swing.JButton();
        btnCart = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(SeatsSelectionView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(755, 700));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lbTicketRate.setFont(resourceMap.getFont("lbTicketRate.font")); // NOI18N
        lbTicketRate.setText(resourceMap.getString("lbTicketRate.text")); // NOI18N
        lbTicketRate.setName("lbTicketRate"); // NOI18N
        add(lbTicketRate, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 88, 44));

        cbTicketRate.setFont(resourceMap.getFont("cbTicketRate.font")); // NOI18N
        cbTicketRate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbTicketRate.setName("cbTicketRate"); // NOI18N
        add(cbTicketRate, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 100, 140, 42));

        tfTicketNumber.setFont(resourceMap.getFont("tfTicketNumber.font")); // NOI18N
        tfTicketNumber.setText(resourceMap.getString("tfTicketNumber.text")); // NOI18N
        tfTicketNumber.setName("tfTicketNumber"); // NOI18N
        add(tfTicketNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 100, 140, 45));

        lbTicketNumber.setFont(resourceMap.getFont("lbTicketNumber.font")); // NOI18N
        lbTicketNumber.setText(resourceMap.getString("lbTicketNumber.text")); // NOI18N
        lbTicketNumber.setMaximumSize(new java.awt.Dimension(146, 22));
        lbTicketNumber.setName("lbTicketNumber"); // NOI18N
        add(lbTicketNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 100, 150, 44));

        btnFindSeats.setFont(resourceMap.getFont("btnFindSeats.font")); // NOI18N
        btnFindSeats.setText(resourceMap.getString("btnFindSeats.text")); // NOI18N
        btnFindSeats.setName("btnFindSeats"); // NOI18N
        btnFindSeats.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindSeatsActionPerformed(evt);
            }
        });
        add(btnFindSeats, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 100, 130, 45));

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, -1, -1));

        btnBook.setFont(resourceMap.getFont("btnBook.font")); // NOI18N
        btnBook.setText(resourceMap.getString("btnBook.text")); // NOI18N
        btnBook.setName("btnBook"); // NOI18N
        btnBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBookActionPerformed(evt);
            }
        });
        add(btnBook, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 180, 130, 70));

        btnCart.setFont(resourceMap.getFont("btnCart.font")); // NOI18N
        btnCart.setText(resourceMap.getString("btnCart.text")); // NOI18N
        btnCart.setName("btnCart"); // NOI18N
        btnCart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCartActionPerformed(evt);
            }
        });
        add(btnCart, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 270, 130, 70));
    }// </editor-fold>//GEN-END:initComponents

    private void btnFindSeatsActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnFindSeatsActionPerformed
    {//GEN-HEADEREND:event_btnFindSeatsActionPerformed
        //this.btnBook.setVisible(true);
        //this.btnCart.setVisible(true);
        TicketRate rate = (TicketRate) this.cbTicketRate.getSelectedItem();

        int ticketNumber = Integer.parseInt(this.tfTicketNumber.getText());
        SeatController sc = new SeatController();

        table = new SeatsTable(sc.getAllAvailableSeats(rate, ticketNumber, event.getEventID()));

        pnlTable.removeAll();
        pnlTable.add(table);
        pnlTable.repaint();
    }//GEN-LAST:event_btnFindSeatsActionPerformed

    private void btnBookActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnBookActionPerformed
    {//GEN-HEADEREND:event_btnBookActionPerformed
        int numberOfSeats = 0;
        JTable dataTable = table.getTable();

        for (int i = 0; i < dataTable.getRowCount(); i++)
        {
            if (Boolean.parseBoolean(dataTable.getValueAt(i, 3).toString()))
            {
                numberOfSeats++;

                Ticket ticket = new Ticket();
                ticket.setTicketRate((TicketRate) this.cbTicketRate.getSelectedItem());
                ticket.setCustomerID(this.getUser().getUserID());
                ticket.setBookedDate(DateTimeHelper.getCurrentDateTime());
                ticket.setIsCancelled(false);
                ticket.setIsPaid(false);

                SeatBO seat = (SeatBO) dataTable.getValueAt(i, 1);
                ticket.setSeat(seat);

                this.getCart().add(ticket);
            }
        }

        if(numberOfSeats == 0)
        {
            JOptionPane.showMessageDialog(pnlTable, "Please select some seats to add");
            return;
        }
        else if(numberOfSeats != 0 && this.getCart().size() != 0)
        {
            JOptionPane.showMessageDialog(pnlTable, "Added successfully");
            return;
        }

    }//GEN-LAST:event_btnBookActionPerformed

    private void btnCartActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCartActionPerformed
    {//GEN-HEADEREND:event_btnCartActionPerformed
        ShoppingCartView scv = new ShoppingCartView(this.getCart(), this.getEvent());
        this.getPnlParent().removeAll();
        this.getPnlParent().add(scv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
        scv.setPnlParent(this.getPnlParent());
    }//GEN-LAST:event_btnCartActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBook;
    private javax.swing.JButton btnCart;
    private javax.swing.JButton btnFindSeats;
    private javax.swing.JComboBox cbTicketRate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbTicketNumber;
    private javax.swing.JLabel lbTicketRate;
    private javax.swing.JTextField tfTicketNumber;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the cart
     */
    public ShoppingCart getCart()
    {
        if(cart == null)
            setCart(new ShoppingCart());
        return cart;
    }

    /**
     * @param cart the cart to set
     */
    public void setCart(ShoppingCart cart)
    {
        this.cart = cart;
    }

    /**
     * @return the event
     */
    public EventView getEvent()
    {
        return event;
    }

    /**
     * @param event the event to set
     */
    public void setEvent(EventView event)
    {
        this.event = event;
    }

    private void initTable()
    {
        Dimension tablePanelSize = new Dimension(580, 500);

        pnlTable = new JPanel();
        pnlTable.setPreferredSize(tablePanelSize);
        pnlTable.setBackground(Color.white);
        add(pnlTable, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, -1, -1));

        //this.btnBook.setVisible(false);
        //this.btnCart.setVisible(false);
    }
}
